package com.hrt.demo.upload;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.util.upload.Plupload;
import com.hrt.util.upload.UploadUtil;

@Controller
@RequestMapping("/manage/demo/mutilUpload")
public class MutiFileUploadController {

	private List<File> files;

	@Autowired
	private UploadUtil uploadUtil;
	
	 public static final String FileDir = "uploadfile/";  
	
	@RequestMapping(value = "/show")
	public ModelAndView show() {

		ModelAndView model = new ModelAndView();

		model.setViewName("manage/demo/upload/mutilUpload/index");
		return model;
	}
	
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public String upload(Plupload plupload,HttpServletRequest request, HttpServletResponse response)
	{
		 //System.out.println(plupload.getChunk() + "===" + plupload.getName() + "---" + plupload.getChunks());  
        
        plupload.setRequest(request);  
        //文件存储路径  
        File dir = new File(plupload.getRequest().getSession().getServletContext().getRealPath("/") + FileDir);  
          
        System.out.println(dir.getPath());  
          
        try {  
            //上传文件  
        	uploadUtil.upload(plupload, dir);  
            //判断文件是否上传成功（被分成块的文件是否全部上传完成）  
            if (UploadUtil.isUploadFinish(plupload)) {  
                System.out.println(plupload.getName()+ "----");  
                String fileFullPath="http://127.0.0.1:8080"+request.getContextPath()+"/"+FileDir+plupload.getName();
                System.out.println("----------------"+fileFullPath);
                File f=new File(fileFullPath);
                if(f!=null && f.exists())
                {
                	System.out.println("http://127.0.0.1:8080/"+f.getAbsolutePath()+" is exist..");
                }
            }else{
            	return "fail";
            }
              
        } catch (IllegalStateException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
          
        return "success";  
    }  
	


	public List<File> getFiles() {
		return files;
	}


	public void setFiles(List<File> files) {
		this.files = files;
	}

}
